<?php


namespace app\common\service;

use Exception;

/**
 * Redis单例类
 * @package app\common\service
 *
 * @Author: Li Guo Xin <guoxinlee129@gmail.com>
 * @Date: 2020/4/28
 * @Time: 15:56
 */
class Redis
{
    private static $redis;

    /**
     * Redis constructor.
     * @throws Exception
     */
    private function __construct()
    {
        try {
            self::$redis = new \Redis();
            self::$redis->connect(config('cache.redis.host'), config('cache.redis.port'));
            self::$redis->auth(config('cache.redis.password'));
            //self::$redis->multi(\Redis::PIPELINE);
            //var_dump(self::$redis->info('redis_version'));die;
            //foreach (self::$redis->info() as $item) {
            //    var_dump($item);
            //}
            //die;
            //self::$redis->setOption(\Redis::OPT_SERIALIZER, \Redis::SERIALIZER_IGBINARY);
            //$a = Db::connect();
            //var_dump($a->table('user1')
            //    ->find());
        } catch (\Exception $e) {
            throw $e;
        }
    }

    public static function getRedis(): \Redis
    {
        if (self::$redis === null) {
            new self();
        }
        return self::$redis;
    }

    //--------------------------------- String start ---------------------------------------
    public static function get($key)
    {
        return self::getRedis()->get($key);
    }
    //--------------------------------- String end ---------------------------------------
}